const express = require('express');

const wechat = require('../weChatOfficialAccount/config/wx')
const sha1 = require('sha1');
const app = express();



app.use(express.json());
app.use(express.urlencoded({extended:true}))

//验证服务器有效性
app.get('/',(req,res)=>{
    let {signature,echostr,timestamp,nonce} = req.query;

    //1.将参数进行堆排序后拼接成字符串
    let str = [wechat.token,timestamp,nonce].sort().join('');
    //2.将字符串进行sha1加密
    let sha = sha1(str);
    //3.将加密后的字符串与signature进行比较
    if(sha === signature){
        res.send(echostr) 
    }
})

//接收用户消息
app.post('/',(req,res)=>{
    console.log(req.body);
    res.send('success')
    
})

app.listen(7099,()=>{
    console.log('Server is running on port 3000')
})